<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class updateArticle extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title'     =>  'required',
            'tag_id'    =>  'required|array|tag',
            'body'      =>  'required',
        ];
    }
    public function messages()
    {
        return [
            'title.required'         =>'标题不得为空',
            'tag_id.required'        =>'标签不得为空',
            'body.required'          =>'内容不得为空',
            'tag_id.array'           =>'标签号必须是数组',
            'tag_id.tag_id'          =>'标签不存在',
            'tag_id.tag'             =>'标签数组中的元素必须是数字',
        ];
    }
    protected function failedValidation(Validator $validator)
    {
        throw(new HttpResponseException(response()->JSON([
            'code' => 100,
            'massages' => $validator->errors()->all(),
        ], 422)->setEncodingOptions(JSON_UNESCAPED_UNICODE)));
    }
}
